package com.android.update.config;

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.text.TextUtils;

import com.alibaba.fastjson.JSON;
import com.android.update.data.DataInfo;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import de.robv.android.xposed.XSharedPreferences;

public class HookDB {

    private static HookDB hookDB;
    public final String DB = "phoneinfo";
    private boolean isXShare = false;

    public static HookDB getIntance(Context context) {
        if (hookDB == null) {
            synchronized (HookDB.class) {
                if (hookDB == null) {
                    if (context == null) {
                        hookDB = new HookDB();
                    } else {
                        hookDB = new HookDB(context);
                    }
                }
            }
        } else {
            hookDB.reload();
        }
        return hookDB;
    }

    public static HookDB getIntance() {
        return getIntance(null);
    }

    private void reload() {
        if (isXShare) {
            ((XSharedPreferences) sharedPreferences).reload();
        }
    }

    private SharedPreferences sharedPreferences;

    @SuppressLint("WrongConstant")
    private HookDB(Context context) {
        isXShare = false;
        sharedPreferences = context.getSharedPreferences(DB, Activity.MODE_WORLD_READABLE);
    }

    private HookDB() {
        isXShare = true;
        XSharedPreferences xSharedPreferences = new XSharedPreferences(Config.packageName, DB);
        xSharedPreferences.makeWorldReadable();
        this.sharedPreferences = xSharedPreferences;
    }


    public void saveData(String content) {
        sharedPreferences.edit().putString("data", content).apply();
    }

    public String getData() {
        reload();
        return sharedPreferences.getString("data", "");
    }

    public void saveBeFiltered(String content) {
        sharedPreferences.edit().putString("beFiltered", content).apply();
    }

    public List<String> getBeFiltered() {
        reload();
        String beFiltered = sharedPreferences.getString("beFiltered", null);
        if (TextUtils.isEmpty(beFiltered)) {
            return new ArrayList<>();
        }
        String[] str = beFiltered.split(",");
        return Arrays.asList(str);
    }

    public void saveFilter(String content) {
        sharedPreferences.edit().putString("filter", content).apply();
    }

    public List<String> getFilter() {
        reload();
        String beFiltered = sharedPreferences.getString("filter", null);
        if (TextUtils.isEmpty(beFiltered)) {
            return new ArrayList<>();
        }
        String[] str = beFiltered.split(",");
        return Arrays.asList(str);
    }

    public DataInfo getDataInfo() {
        reload();
        String content = sharedPreferences.getString("data", "");
        if (TextUtils.isEmpty(content)) {
            return DataInfo.getDefault();
        }
        return JSON.parseObject(content, DataInfo.class);
    }
}
